package com.sz.controller;

import com.alibaba.fastjson.JSON;
import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.sz.model.OnlineBankCard;
import com.sz.model.OnlineBankTransferInfo;
import com.sz.model.Result;
import com.sz.service.IOnlineBankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/obc")
public class OnlineBankCardController {

    @Autowired
    private IOnlineBankCardService iOnlineBankCardService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //判断网银银行卡余额
    @RequestMapping(value = "/amount",method = RequestMethod.POST)
    public Result getBankCardAmount(@RequestParam String amount, @RequestParam String account){
        Result result=null;
        //余额
        Long aLong =iOnlineBankCardService.selectMoney(account);

        int money=Integer.parseInt(amount);
        if(aLong!=null&&aLong>money){
            result =new Result(200,"余额充足");
        }else{
            result =new Result(301,"余额不足");
        }
        return result;
    }


    //网银转账
    @LcnTransaction//分布式事务调用者
    @RequestMapping("/transfer")
    public Result OnlineBankTransfer(@RequestBody OnlineBankTransferInfo onlineBankTransferInfo){
        System.out.println("onlineBankTransferInfo.toString() = " + onlineBankTransferInfo.toString());
        Result result=null;

        int i=0;
        String pcode = stringRedisTemplate.opsForValue().get(onlineBankTransferInfo.getPhone());
        if(pcode.equals(onlineBankTransferInfo.getPhoneCode())){

            //查询密码
            String payPwd = iOnlineBankCardService.getBankCard(onlineBankTransferInfo.getPayerAccount()).getPayPwd();

            if(onlineBankTransferInfo!=null&&payPwd.equals(onlineBankTransferInfo.getPayPwd())){

                i=iOnlineBankCardService.onlineBankTransfer(onlineBankTransferInfo.getPayerAccount(),onlineBankTransferInfo.getPayeeAccount(),onlineBankTransferInfo.getTradingAmount());

                if(i==2){
                    result=new Result(200,"转账成功");
                }else{
                    result=new Result(304,"转账失败");
                }

            }else{
                System.out.println("密码错误");
                result=new Result(305,"密码错误");
            }
        }else{
            result=new Result(306,"验证码错误");
        }

        System.out.println("i = " + i);

        return result;
    }


    //添加银行卡
    @RequestMapping("/addcard")
    public Result addBankCard(@RequestBody OnlineBankCard onlineBankCar){
        Result result=null;
        OnlineBankCard bankCard = iOnlineBankCardService.getBankCard(onlineBankCar.getObcAccount());

        if(bankCard!=null){
            result = new Result<>(302, "账户已存在，添加失败");
        }else{
            onlineBankCar.setObcBalance(new Long(10000));
            int i = iOnlineBankCardService.insertBankCart(onlineBankCar);
            if(i==1) {
                result = new Result<>(200, "添加成功");
            }else{
                result = new Result<>(301, "添加失败，请稍后重试");
            }
        }

        return result;
    }

    //查询银行卡金额
    @RequestMapping("/getbalance")
    public Result getBalabce(@RequestBody String card){
        Result result=null;

        OnlineBankCard bankCard = iOnlineBankCardService.getBankCard(card);
        result=new Result<>(200,"succes",bankCard);

        return result;
    }

    //获取所有银行卡列表
    @RequestMapping("/cardlist")
    public Result getAllBankCArd(@RequestBody int obid){
        Result result=null;

        String allBankCard1 = stringRedisTemplate.opsForValue().get("allBankCard");
        //redis判断
        List<OnlineBankCard> allBankCard=null;
        if(allBankCard1!=null&&allBankCard1!=""){

            allBankCard = JSON.parseArray(allBankCard1, OnlineBankCard.class);

        }else{//redis不存在
            //查询所有，用户的银行卡list
            allBankCard = iOnlineBankCardService.getAllBankCard(obid);

            String s = JSON.toJSONString(allBankCard);

            stringRedisTemplate.opsForValue().set("allBankCard",s,1,TimeUnit.HOURS);
        }

        result=new Result(200,"获取成功",allBankCard);

        return result;
    }


    //获取银行卡列表2
    @RequestMapping("/cardlist2")
    public Result getAllBankCArd2(@RequestParam("obid") String obid, @RequestParam("card") String card){
        System.out.println("obid = " + obid);
        Result result=null;
        //redis获取list的json字符串
        String allBankCard1 = stringRedisTemplate.opsForValue().get("allBankCard");

        List<OnlineBankCard> allBankCard= JSON.parseArray(allBankCard1, OnlineBankCard.class);
        //遍历移除已选择的银行卡
        for (int i = 0; i <allBankCard.size() ; i++) {
            OnlineBankCard onlineBankCard = allBankCard.get(i);
            if(onlineBankCard.getObcAccount().equals(card)){
                allBankCard.remove(i);
            }
        }

        System.out.println("allBankCard.toString() = " + allBankCard.toString());
        result=new Result(200,"获取成功",allBankCard);

        return result;
    }

    @RequestMapping("/onbanktran")
    public Result RegOnBankTran(String payerAccount,Long tradingAmount ){
        Result result=null;
        int i = iOnlineBankCardService.RegTranBank(payerAccount, tradingAmount);

        System.out.println("---------------");
        if(i==1){
            result=new Result(200,"成功");
        }else{
            result=new Result(301,"失败");
        }
        return result;
    }
}
